import {Injectable} from '@angular/core';
import {HttpClient, HttpParams} from '@angular/common/http';
import {Observable} from 'rxjs';
import {configParams} from '../../../storage-config';

@Injectable({
  providedIn: 'root'
})
export class CommodityCategoryService {

  // 添加分类
  public ADD_URL = '/whGoodsType/add';
  // 分类列表
  public GET_URL = '/whGoodsType/list/chart';
  // 获取分类item
  public GETITEM_URL = '/whGoodsType/get/';
  // 分类下拉
  public GETLIST_URL = '/whGoodsType/list/chart/';
  // 分类删除
  public DELETE_URL = '/whGoodsType/remove/';
  // 分类更新
  public UPDATE_URL = '/whGoodsType/update';

  constructor(public httpClient: HttpClient) {
  }

  POST(url: any, body: any) {
    return this.httpClient.post(url, body, {headers: configParams.headers});
  }

  GET(url: any, params: any = {}) {
    return this.httpClient.get(url, {params, headers: configParams.headers});
  }

  /**
   * 添加分类
   * @param json 参数
   */
  public add(json: any | null): Observable<any> {
    delete json.id;
    json.type == 0 ? delete json.url : null;
    return this.POST(this.ADD_URL, json);
  }

  /**
   * 分类列表
   * @param json 参数
   */
  public get(): Observable<any> {
    return this.GET(`${this.GET_URL}?isList=0`);
  }

  /**
   * 分类列表
   * @param json 参数
   */
  public getList(id: any | null): Observable<any> {
    const url = `${this.GETLIST_URL}${id}`;
    return this.GET(url);
  }

  /**
   * 根据ID获取分类详情
   * @param json 参数
   */
  public getItem(id: any | null): Observable<any> {
    const url = `${this.GETITEM_URL}${id}`;
    return this.GET(url);
  }

  /**
   * 更新分类
   * @param json 参数
   */
  public update(json: any | null): Observable<any> {
    json.type == 0 ? delete json.url : null;
    return this.POST(this.UPDATE_URL, json);
  }

  /**
   * 删除分类
   * @param json 参数
   */
  public delete(id: any | null): Observable<any> {
    const url = `${this.DELETE_URL}${id}`;
    return this.GET(url);
  }

}
